/*
 * 更新入库表
 * 参数   交易码：trancode=51116
 * 
 */

package org.ccams.tran;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TpInDetail;
import org.ccams.domain.TpInHead;
import org.ccams.web.WebBeanFactory;
import org.json.JSONArray;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;


public class Tran51116 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		TpInHead tih = new TpInHead();
		
		Long numId =Long.parseLong( request.getParameter("numId"));//取得记录ID
		// 获得用户录入信息
		String prov_name=(String) request.getParameter("prov_name");
		System.out.println("供应商:"+prov_name);
		Date operdate = new SimpleDateFormat("yyyy-MM-dd").parse((String) request.getParameter("operdate"));
		System.out.println("验收日期:"+operdate);
		
		String in_no=(String) request.getParameter("in_no");
		System.out.println("单据编号:"+in_no);
		String tran_kind = (String) request.getParameter("tran_kind");
		Short tranKind;
		if(tran_kind.equals("供应商自付")){
			tranKind = 0;
		}else if(tran_kind.equals("我方垫付")){
			tranKind = 1;
		}else{
			tranKind = Short.parseShort(tran_kind);
		}		
		System.out.println("支付方式:"+tran_kind);
		Double tran_money = 0d;
		try{
			tran_money= Double.parseDouble((String) request.getParameter("tran_money"));
		}catch(Exception e){}
		System.out.println("运费:"+tran_money);
		String uname = (String) request.getParameter("uname");
		System.out.println("相关部门:"+uname);
		String operName = (String) request.getParameter("operName");		
		System.out.println("操作员:"+operName);
		String operUName = (String) request.getParameter("operUName");
		System.out.println("操作员部门:"+operUName);
		String remark = (String) request.getParameter("remark");
		System.out.println("备注:"+remark);
		try
		{
			Date cw_date=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("cw_date"));
			tih.setCw_date(cw_date);
			System.out.println("财务日期:"+cw_date);
		}catch(Exception e){}
		
		tih.setNum(numId);
		tih.setProv_name(prov_name);
		tih.setOperdate(operdate);
		tih.setIn_no(in_no);
		tih.setTran_kind(tranKind); 
		tih.setTran_money(tran_money);
		tih.setUname(uname);
		tih.setOperName(operName);
		tih.setOperUName(operUName);
		tih.setRemark(remark);
		
		//获得详细信息
		String params = (String) request.getParameter("detail");
		
		JSONArray detailParams = new JSONArray(params);
		
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		String success = "";
		String message = "";	
		int k=0;		
		DataSourceTransactionManager txm = (DataSourceTransactionManager) WebBeanFactory.getBean("transactionManager",request);
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = txm.getTransaction(def);
		try{
			k = getCcamsService().getTpinheadDao().updateTpinhead(tih);//更新入库主表信息
			for(int i=0;i<detailParams.length();i++){
				org.json.JSONObject detail = detailParams.optJSONObject(i);
				TpInDetail tid = new TpInDetail();
				tid.setNum(numId);
				tid.setMaterielName(detail.getString("materielName"));
				tid.setBatchno(detail.getString("batchno"));
				tid.setKind(Short.parseShort(detail.getString("kind")));
				tid.setType(Short.parseShort(detail.getString("type")));
				tid.setSerialNo(detail.getString("serialNo"));
				tid.setFactcode(detail.getString("factcode"));
				tid.setOtlcode(detail.getString("otlcode"));
				tid.setBatchid(detail.getInt("batchid"));
				tid.setAmount(detail.getLong("amount"));
				tid.setIn_price(detail.getDouble("in_price"));
				tid.setTax_money(detail.getDouble("tax_money"));
				tid.setPrice(detail.getDouble("price"));
				tid.setOut_price(detail.getDouble("out_price"));
				tid.setProd_date(new SimpleDateFormat("y-M-d").parse(detail.getString("prod_date")));
				tid.setRemark(detail.getString("remark"));
				System.out.println("=================================");
				System.out.println("单据ID 	:	" +tid.getNum());			
				System.out.println("物料名称 	:	" +tid.getMaterielName());			
				System.out.println("批号 	:	" +tid.getBatchno());			
				System.out.println("采购类型	:	" +tid.getKind());			
				System.out.println("入库类型 	:	" +tid.getType());			
				System.out.println("机身码 	:	" +tid.getSerialNo());			
				System.out.println("厂家条码 	:	" +tid.getFactcode());			
				System.out.println("OTL条码 	:	" +tid.getOtlcode());			
				System.out.println("批次 	:	" +tid.getBatchid());			
				System.out.println("入库数量 	:	" +tid.getAmount());			
				System.out.println("入库价格 	:	" +tid.getIn_price());			
				System.out.println("税额		:	" +tid.getTax_money());			
				System.out.println("零售价 	:	" +tid.getPrice());			
				System.out.println("批发价 	:	" +tid.getOut_price());			
				System.out.println("生产日期 	:	" +tid.getProd_date());			
				System.out.println("备注 	:	" +tid.getRemark());	
				boolean b = detail.has("ord");
				if(b){
					tid.setOrd(Integer.parseInt(detail.getString("ord")));
					k = getCcamsService().getTpindetailDao().updateTpindetail(tid);//执行更新
				}else{
					int ord = getCcamsService().getTpindetailDao().getNewOrd(numId);
					tid.setOrd(ord);
					k = getCcamsService().getTpindetailDao().insertTpindetail(tid);//执行添加	
				}
			}
		}
		catch(Exception ex){
			txm.rollback(status);
	        ex.printStackTrace();
		}finally{
			txm.commit(status);
		}
		if (k == 1) {
			success = "true";
			message = "入库更新成功";
		} else {
			success = "false";
			message = "入库更新失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
